import numpy as np

def init_random_image(img):
	return np.random.choice([0, 1], size=img.shape)

def calculate_err(img, new_img):
	return np.sum((img - new_img) ** 2)

def dbs(img: list, max_iter=10000):
	h, w = img.shape
	img = np.array(img)/255.0  # randomize
	out_img = init_random_image(img)
	
	for _ in range(max_iter):
		i, j = np.random.randint(0, h), np.random.randint(0, w)
		
		curr_err = calculate_err(img, out_img)
		# convert pixsel
		out_img[i, j] = 1 - out_img[i, j]
		new_err = calculate_err(img, out_img)
		
		if new_err > curr_err:
			out_img[i, j] = 1 - out_img[i, j]
	
	return np.array(out_img) * 255.0

